/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import ecom.beans.AccountBean;
import ecom.beans.EcomCustomerRemote;

/**
 *
 * @author Sebastien
 */
public class Account extends AbstractPage {

    private String login,  pass,  redirectAction,  referer;
    private AccountBean account;
    private double depositAmount;

    public String login() throws Exception {
        return (referer.contains("ProductBy") || referer.contains("Cart")) ? "redirectToReferrer" : "home";
    }

    public String logout() throws Exception {
        this.getSession().put("user", null);
        return "home";
    }

    public String authBeforeBuy() throws Exception {
        AccountBean user = (AccountBean) getSession().get("user");
        if (user != null) {
            return "cart";
        }
        this.setPage("login_or_create_account.jsp");
        return SUCCESS;
    }

    public String deposit() throws Exception {
        AccountBean user = (AccountBean) getSession().get("user");
        if (user == null) {
            throw new Exception("Please, log in to do that !");
        }
        String[] requestAction = (String[]) getParameters().get("action");
        EcomCustomerRemote remoteBean = (EcomCustomerRemote) getSession().get("customerRemote");
        if (requestAction != null && "deposit".equalsIgnoreCase(requestAction[0])) {
            remoteBean.deposit(user.getAccountId(), depositAmount);
            user.deposit(depositAmount);
            return "redirectToReferrer";
        } else {
            account = user;
            this.setPage("deposit_cash.jsp");
            return SUCCESS;
        }
    }

    public String addOrUpdate() throws Exception {
        String[] requestAction = (String[]) getParameters().get("action");
        EcomCustomerRemote remoteBean = (EcomCustomerRemote) getSession().get("customerRemote");
        AccountBean user = (AccountBean) getSession().get("user");

        // update
        if (requestAction != null && "create".equalsIgnoreCase(requestAction[0])) {
            remoteBean.createAccount(account.getAccountOwner(),
                    account.getBalance(), account.getLogin(),
                    account.getPass(), account.getSurname(),
                    account.getAddress(), account.getBirthdate());
            this.login = account.getLogin();
            this.pass = account.getPass();

            user = remoteBean.getAccountByCredential(login, pass);
            this.getSession().put("user", user);

            return "DepositCash";
        } else if (requestAction != null && "update".equalsIgnoreCase(requestAction[0])) {
            if (user == null) {
                throw new Exception("Please, log in to do that !");
            }
            remoteBean.updateAccount(user.getAccountId(), user.getAccountOwner(),
                    user.getBalance(), user.getLogin(),
                    account.getPass(), user.getSurname(),
                    account.getAddress(), account.getBirthdate());

            user.setAddress(account.getAddress());
            user.setPass(account.getPass());
            user.setBirthdate(account.getBirthdate());

        } // affichage du formulaire pré-rempli
        else {
            String[] newOrUpdate = (String[]) getParameters().get("new");
            if (newOrUpdate != null) {
                this.setPage("account_create.jsp");
            } else {
                account = user;
                this.setPage("account_update.jsp");
            }
            return SUCCESS;
        }
        return "home";
    }

    @Override
    public void validate() {
        String[] requestAction = (String[]) getParameters().get("action");

        if (requestAction != null && "create".equalsIgnoreCase(requestAction[0])) {
            this.setPage("account_create.jsp");

            if (account.getAccountOwner().length() == 0) {
                this.addFieldError("account.accountOwner", "Préciser le nom");
            }
            if (account.getSurname().length() == 0) {
                this.addFieldError("account.surname", "Préciser le prénom");
            }
            if (account.getAddress().length() == 0) {
                this.addFieldError("account.address", "Préciser l'addresse");
            }
            if (account.getLogin().length() == 0) {
                this.addFieldError("account.login", "Préciser le login");
            }
            if (account.getPass().length() == 0) { 
                this.addFieldError("account.pass", "Préciser le pass");
            }
            if (account.getPass().length() < 5) {
                this.addFieldError("account.pass", "Veuillez saisir un mot de passe de plus de 5 caracteres");
            }

        } else if (requestAction != null && "update".equalsIgnoreCase(requestAction[0])) {
            this.setPage("account_update.jsp");
            AccountBean loggedUser = (AccountBean) getSession().get("user");
            if (account.getAddress().length() == 0) {
                this.addFieldError("account.address", "Préciser l'addresse");
                account = loggedUser;
            }
            if (account.getPass().length() == 0) {
                this.addFieldError("account.pass", "Préciser le pass");
                account = loggedUser;
            }
            if (account.getPass().length() < 5) {
                this.addFieldError("account.pass", "Veuillez saisir un mot de passe de plus de 5 caracteres");
            }
        } else if (requestAction != null && "deposit".equalsIgnoreCase(requestAction[0])) {
            this.setPage("deposit_cash.jsp");
            account = (AccountBean) getSession().get("user");

            if (depositAmount > 1500.0) {
                this.addFieldError("depositAmount", "Le montant max d'une transaction est 1500.0 euros");
            }

        } else if (requestAction != null && "login".equalsIgnoreCase(requestAction[0])) {
            this.setPage("welcome.jsp");

            if (login.length() == 0) {
                this.addFieldError("login", "Préciser le login");
            }
            if (pass.length() == 0) {
                this.addFieldError("pass", "Préciser le mot de passe");
            }
            if (login.length() != 0 && pass.length() != 0) {
                EcomCustomerRemote ecomCustomerBean = (EcomCustomerRemote) getSession().get("customerRemote");
                AccountBean user = ecomCustomerBean.getAccountByCredential(login, pass);

                if (user == null) {
                    this.addFieldError("login", "IDENTIFICATION ECHOUEE");
                } else {
                    this.getSession().put("user", user);
                }
            }
        }
    }

    public AccountBean getAccount() {
        return account;
    }

    public void setAccount(AccountBean account) {
        this.account = account;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    public String getRedirectAction() {
        return redirectAction;
    }

    public void setRedirectAction(String redirectAction) {
        this.redirectAction = redirectAction;
    }

    public double getDepositAmount() {
        return depositAmount;
    }

    public void setDepositAmount(double depositAmount) {
        this.depositAmount = depositAmount;
    }

    public void setReferer(String referer) {
        this.referer = referer;
    }
}
